import cTitle from "components/title";
import { Toast } from "vant";
export default {
  data() {
    return {
      faceServetitle: "serve-title",
      query: {
        title: "",
        recharge_member_mobile: "", //充值会员手机
        recharge_merchant_mobile: "",
        recharge_total: 0,
        amount: "",
        points: 0
      },
      amount: "",
      setting: {},
      merchant_love_value: '0.00',
      checked: false,
      flag: false,
      isActive: false,
      shopsActive: false,
      errorText: "",
      ShopsErrorText: "",
      btnFlag:false
      
    };
  },
  activated() {
    this.orderSetting();
  },
  computed: {
  },
  methods: {
    monitorMoblie(value) {
      let res = /^[1][3,4,5,6.7,8,9][0-9]{9}$/;
      // let value = event.target._value;
      if (res.test(value)) {
        this.member(value, "consumer");
        this.isActive = false;
      } else {
        this.isActive = true;
        this.errorText = "请填写正确的手机号";
      }
    },
    merchantMoblie(value) {
      let res = /^[1][3,4,5,6.7,8,9][0-9]{9}$/;
      // let value = event.target._value;
      if (res.test(value)) {
        this.member(value, "shops");
        this.shopsActive = false;
      } else {
        this.shopsActive = true;
        this.ShopsErrorText = "请填写正确的手机号";
      }
    },
    changeprice(event) {
      console.log(event);
      this.amount = this.amount.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3");
      let data = [this.amount,this.setting.pay_ratio];
      
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.multiplication(data));
      }, 500);
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    },
    multiplication(data) {
      $http
        .post("plugin.face-to-face.frontend.recharge-order.multiplication", {data: data}, "")
        .then(res => {
          if (res.result === 1) {
            this.query.amount = res.data;
            let dataLove = [this.setting.merchant_love_value,this.amount,this.setting.integral_love_ratio?.ratio];
            if(this.setting?.merchant_love === "ratio") {
              this.multiplicationLove(dataLove);
            }
            
          } else {
            Toast(res.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    multiplicationLove(data) {
      $http
        .post("plugin.face-to-face.frontend.recharge-order.multiplication", {data: data}, "")
        .then(res => {
          if (res.result === 1) {
            this.merchant_love_value = res.data;
          } else {
            Toast(res.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    orderPay() {
      if (!this.query.title) {
        Toast("请输入商品名称或服务名称");
        return;
      }
      if (!this.query.recharge_total) {
        Toast("请输入商品数量");
        return;
      }
      if (!this.query.recharge_member_mobile) {
        Toast("请输入会员手机号");
        return;
      }
      if (!this.query.recharge_merchant_mobile) {
        Toast("请输入商家会员手机号");
        return;
      }
      if (!this.query.amount) {
        Toast("请输入金额");
        return;
      }
      if (this.isActive) {
        Toast(this.errorText);
        return;
      }
      if (this.shopsActive) {
        Toast(this.ShopsErrorText);
        return;
      }
      if (!this.checked) {
        Toast("请勾选协议");
        return;
      }
      this.query.amount = this.query.amount.toString();
      this.query.points = this.amount.toString();
      this.flag = true;
      $http
        .post("plugin.face-to-face.frontend.recharge-order.create", this.query, "")
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            let orderJson = {
              order_ids: data.order_ids
            };
            setTimeout(() => {
              this.flag = false;
            },1000);
            this.$router.push(this.fun.getUrl("orderpay", orderJson, { api: "plugin.face-to-face.frontend.recharge-order.index" }));
          } else {
            Toast(res.msg);
            setTimeout(() => {
              this.flag = false;
            },1000);
          }
        })
        .catch(error => {
          setTimeout(() => {
            this.flag = false;
          },1000);
          console.log(error);
        });
    },
    member(mobile, name) {
      $http
        .post("plugin.face-to-face.frontend.recharge-order.verify-member", { mobile }, "")
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            if (name === "consumer") {
              this.query.recharge_member_mobile = data.mobile;
              this.errorText = "";
            } else {
              this.query.recharge_merchant_mobile = data.mobile;
              this.ShopsErrorText = "";
            }
          } else {
            name === "consumer" ? (this.isActive = true) : (this.shopsActive = true);
            name === "consumer" ? (this.errorText = res.msg) : (this.ShopsErrorText = res.msg);
            return false;
          }
        })
        .catch(error => {
          console.log(error);
          return false;
        });
    },
    orderSetting() {
      $http
        .get("plugin.face-to-face.frontend.recharge-order.set", {}, "")
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            this.setting = data;
            
            if(data.merchant_love === "fixed") {
              this.merchant_love_value = data.merchant_love_value;
            } 
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  components: { cTitle }
};
